package com.example.exampleOS.slice;

import com.example.exampleOS.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ProgressBar;
import ohos.agp.components.webengine.BrowserAgent;
import ohos.agp.components.webengine.WebAgent;
import ohos.agp.components.webengine.WebConfig;
import ohos.agp.components.webengine.WebView;
import ohos.media.image.PixelMap;

public class WebViewSlice extends AbilitySlice {
    private WebView webView;
    private ProgressBar progressBar;//进度条
    private Component loadingView;//创建 加载窗口
    private Component parse;//创建 加载窗口
    private DirectionalLayout webViewBox;//
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_web_view);
        configWebView();

    }
    private void configWebView(){
        DirectionalLayout componentById =(DirectionalLayout) findComponentById(ResourceTable.Id_webviewbox);
        parse = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_web_view, null, false);
        componentById.addComponent(parse);
        Component loadingViewparse = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_view_loading, null, false);
        webView = (WebView) parse.findComponentById(ResourceTable.Id_webview);
        progressBar = (ProgressBar) parse.findComponentById(ResourceTable.Id_progressbar);
        loadingView = loadingViewparse.findComponentById(ResourceTable.Id_loadingView);
        webView.load("https://www.baidu.com");
        WebConfig webConfig = webView.getWebConfig();
        webConfig.setJavaScriptPermit(true);//设置是否在WebView中启用JavaScript。
        webConfig.setDataAbilityPermit(true);//设置是否允许访问WebView中的Data能力。
        webConfig.setWebStoragePermit(true);//设置是否启用HTML5 DOM存储
        webConfig.setViewPortFitScreen(true);//设置视口是否适合屏幕宽度。
        webConfig.setLocationPermit(true);
        webView.setWebAgent(new WebAgent() {
            //在页面加载开始时调用
            @Override
            public void onLoadingPage(WebView webView, String url, PixelMap icon) {
                super.onLoadingPage(webView, url, icon);
                progressBar.setVisibility(Component.VISIBLE);
                loadingView.setVisibility(Component.VISIBLE);
            }
            //当页面加载停止时调用
            @Override
            public void onPageLoaded(WebView webView, String url) {
                super.onPageLoaded(webView, url);
                progressBar.setVisibility(Component.HIDE);
                loadingView.setVisibility(Component.HIDE);
            }
        });
        //设置指定的BrowserAgent对象，该对象用于处理与网页相关的回调，如处理JavaScript事件和网站属性更改事件。
        webView.setBrowserAgent(new BrowserAgent(getContext()){
            //当页面加载进度更新时调用
            @Override
            public void onProgressUpdated(WebView webView, int newValue) {
                super.onProgressUpdated(webView, newValue);
                progressBar.setProgressValue(newValue);
            }
        });


    }
    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    protected void onInactive() {
        super.onInactive();
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackground() {
        super.onBackground();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}
